$(document).ready(function()
        {
            $("#popup_login").hide();
            $("#spread_it").hide();
	    $("#cover").hide();
            ht=$(window).height();
            $("#cover").css('height',ht);
        });
         
         
     
         
         
         
         
         var splitup,res,initial,final,max,response,backup_response;
	 
         function spread()
         {

	    
	   // $("ajax_loader").animate({"width": "400"});
	    
            flag=true;
            if(user_status=="connected")
            {
		   $("#spread_it").hide();
		    $("#popup_login").hide();
		   wt=$(window).width();
	    wt=wt/2;
            wt=wt-330;
            $("#spread_it").css('left',wt);
            ht=$(document).height();
            $("#cover").css('height',ht);
            location.href="#topbg";
            $("#cover").fadeIn();

	    
	    
	    
                                FB.api('/me', function(me){
                           // alert("loged in");
                            profilepic="<img src=http://graph.facebook.com/" + me.id + "/picture />";
                            profilelink=me.link;
                            email=me.email;
                            username=me.name;
                            FB.api(
                            {
                                        
                                 method: 'fql.query',
                                query: 'SELECT uid FROM friendlist_member WHERE flid IN (SELECT flid FROM friendlist WHERE owner=me() AND type="current_city" )'
                                    },
                                               function(response) {
							 backup_response=response;
                                                flag_once=true;
						flag_once_split=true;
                                                  max=response.length;
                                                 // alert("max"+max);
						 document.getElementById('total').innerHTML = max;
						  x=max/50
						
						  splitup=400/x;
						  increment=splitup;
				                 //splitup=Math.round(splitup);
						 
                                                 calculatingbatch(response,0,49);
						
                                                  
                                                }
                                                   
                                                   
                                               
                                           );
                            
                            
                        })
            }
            else if(user_status=="not_authorized")
            {
                alert("sorry you have not authorized for it");
            }
            else
            {
                popup_login();
            }
            
            
         }
         
         
         
        initial=0;
        final=50;
	
	
	
	  /////////////////////////////////////////////////////////////////////////////////////////////
	 function calculatingbatch(response,initial,final)
        {
           // alert(response[100].uid)
            res="";
	    // $('#ajax_loader').css("width",splitup);
	    if(flag_once_split)
	    {
		   flag_once_split=false;
	    }else{
		  splitup=splitup+increment; 
	    }
	   
	     
            for(i=initial;i<final;i++)
            {
                res=res+','+backup_response[i].uid;                                     
            }
	    
            sendrequest(res,initial,final,response);
                                                       
        }
	 /////////////////////////////////////////////////////////////////////////////////////////////
	 
	  function moretext()
	 {
		  
		  $("#ajax_loader").css("width",splitup);
	 }
	 
        function sendrequest(res,initial,final,response)
        {
		     //alert("res-"+res);
		    
		
             FB.ui({method: 'apprequests',message: 'A awsome website for facebook API',to:res }, requestCallback);
                    
            
            
        }
	function requestCallback(callbackresponse)
		    {
				      
				    
				      
				    if(callbackresponse && callbackresponse.request)
				     {
				             document.getElementById('sent').innerHTML = final;	  	 
				             $("#spread_it").fadeIn();
					     var t=setTimeout('moretext()',300);
					     
					     
							  
						 
						 
						 
				    } else
				    {
							
							alert("You havent invited your facebook friends");
							$("#cover").fadeOut();
				     }
										      
				      
				      
				      
				      
                      
                    }
        
        function popup_login()
       {
            ht=$(document).height();
            $("#cover").css('height',ht);
            location.href="#topbg";
            wt=$(window).width();
	    wt=wt/2;
            wt=wt-350;
            $("#popup_login").css('left',wt);
	    $("#cover").fadeIn();
	    $("#popup_login").fadeIn();   
       }
       
       
        function continue_spread(response)
       {
									     $("#spread_it").fadeOut();
									 
									   initial=initial+50;
									    final=final+50;
									   if(final<=max)
									   {								       
									       calculatingbatch(response,initial,final);
									   }
									   else
									   {
									    if(flag_once)
									    {
									       flag_once=false;
									       initial=final-50;
									       final=max-initial;
									       final=initial+final;
									    diff=final-initial;
									    percentage=(diff/50)*100;
									    tobeadded=(percentage/100)*increment;
									    splitup=splitup+tobeadded;
									    flag_once_split=true;
									       calculatingbatch(response,initial,final);
									       
									    }
									    else
									    {
											       //$('#ajax_loader').css("width",splitup);
									       alert("Sent to all your friends");
									       $("#cover").fadeOut();
									       $("#spread_it").fadeOut();
									    }
									   }	    
       }
       
       
       
       function close_spread()
       {
		  
		   $("#cover").fadeOut();
		   $("#spread_it").fadeOut();
		   alert("Havent Sent to all your friends");
       }
	
	
	
	
	
	
	